//
//  ViewController.swift
//  NSAttributedString-AttributedString
//
//  Created by 李煜 on 2024/9/12.
//

import UIKit

class ViewController: UIViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    useNSAttributedString()
    useNSMutableAttributedString()
    useAttributedString()
  }

  func useNSAttributedString() {
    // 创建 UILabel 并允许其显示多行
    let label = UILabel()
    label.numberOfLines = 0 // 设置为 0 以允许多行
    label.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(label)

    // 设置 UILabel 的约束
    NSLayoutConstraint.activate([
      label.centerXAnchor.constraint(equalTo: view.centerXAnchor),
      label.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -100),
      label.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
      label.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
    ])

    // 创建一个多行

    let text = "Hello, world!"
    let attributes: [NSAttributedString.Key: Any] = [
      .font: UIFont.boldSystemFont(ofSize: 20),
      .foregroundColor: UIColor.red,
    ]

    let attributedString = NSAttributedString(string: text, attributes: attributes)
    // 将 AttributedString 设置为 UILabel 的 attributedText
    label.attributedText = attributedString
  }

  // MARK: - 使用NSMutableAttributedString

  func useNSMutableAttributedString() {
    let fullText = "Hello, world!\n这是第二行\n这是第三行\n这是第四行"
    // 创建
    let muAttStr = NSMutableAttributedString(string: fullText)

    // 特定范围高亮
    let highlightedRange = (fullText as NSString).range(of: "这是第三行")
    muAttStr.addAttributes([.foregroundColor: UIColor.green,
                            .font: UIFont.systemFont(ofSize: 20)], range: highlightedRange)

    // 设置字体和颜色
    muAttStr.addAttributes([
      // 粗体+斜体
      .font: UIFont(descriptor: UIFontDescriptor().withSymbolicTraits([.traitBold, .traitItalic])!, size: 16),
      .foregroundColor: UIColor.red,
      .underlineStyle: NSUnderlineStyle.single.rawValue,

    ], range: NSRange(location: 0, length: 13))
    muAttStr.addAttributes([.backgroundColor: UIColor.yellow], range: NSMakeRange(7, 5))
    // 追加文本并设置不同的属性
    let additionalText = NSAttributedString(string: " Welcome to NSAttributedString!", attributes: [
      .font: UIFont.italicSystemFont(ofSize: 16),
      .foregroundColor: UIColor.blue,
    ])

    muAttStr.append(additionalText)

    let label = UILabel()

    label.numberOfLines = 0
//    label.backgroundColor = .gray
    view.addSubview(label)
    label.attributedText = muAttStr
    label.sizeToFit()
    label.frame.origin = CGPointMake(20, 200)
  }

  // MARK: - useAttributedString

  func useAttributedString() {
    let compLb = UILabel()
    compLb.translatesAutoresizingMaskIntoConstraints = false
    compLb.numberOfLines = 0
    view.addSubview(compLb)

    NSLayoutConstraint.activate([
      compLb.centerXAnchor.constraint(equalTo: view.centerXAnchor),
      compLb.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 100),
      compLb.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
      compLb.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
    ])

    var att = AttributedString("THIS IS A MULTILINE TEXT:\nfirst line with a highlighted text.\nsecond line with italic style.\nthird line with foreground orange color.")
    att.font = UIFont.systemFont(ofSize: 24, weight: .light)
    att.foregroundColor = UIColor.lightGray

    // 1.设置高亮
    if let range = att.range(of: "highlighted") {
      att[range].backgroundColor = UIColor.blue
      att[range].foregroundColor = UIColor.white
    }
    // 2.设置粗体+斜体
    if let range = att.range(of: "italic style") {
      att[range].foregroundColor = UIColor.red

      // 系统没提供加粗斜体方法
      // 可以使用fontDescriptor创建组合特性的字体:粗体+斜体
      // 如下创建一个系统字体40号+粗体+斜体
      if let des = UIFont.systemFont(ofSize: 40).fontDescriptor.withSymbolicTraits([.traitBold, .traitItalic]) {
        att[range].font = UIFont(descriptor: des, size: 30)
      }
    }
    // 3.设置前景色
    if let range = att.range(of: "foreground orange color") {
      att[range].foregroundColor = UIColor.orange
    }
    // 4.append方式添加一个attributedString
    var lastAtt = AttributedString("append a attributedString")
    lastAtt.foregroundColor = UIColor.systemBlue
    // 使用append方法
    att.append(lastAtt)

    // 赋值到label
    compLb.attributedText = NSAttributedString(att)
  }
}
